<?php
class Controller_Admin_Category extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'category';
        parent::__construct($req, $res);

        //$this->_list_where = array(
        //    array('type', '=', 'SUBCAT'),
        //);
    }

    public function action_index ()
    {
        $this->action_list();
    }

    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $post['created']=time();
            $this->_form_data = $post;
        }

        parent::action_create();
    }

    public function action_update ($nid=null)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $post['changed']=time();
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
            $this->_form_data['iconUrl'] = explode('|', $data['iconUrl']);
        }

        parent::action_update($nid);
    }

    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        $this->_form_data['iconUrl'] = explode('|', $data['iconUrl']);

        parent::action_detail($nid);
    }

    private function rebuild_post_data (& $post)
    {
        //
        $post['uid'] = user::current_user_id();

        if (! isset($post['iconUrl']))
            $post['iconUrl'] = array();
            
        $post['iconUrl'] = implode('|', $post['iconUrl']);
    }

    public function list_get_data ($offset, $limit)
    {
        return App_Category::all_categories($offset, $limit, FALSE, 0);

        return NULL;
    }


    public function classname_by_list_row (& $row)
    {
    }

    public function actions_by_list_row (& $row, & $menus)
    {

    }
}


